package cn.magicman.socket;

import java.io.*;
import java.net.*;

public class SimpleClient {
    public static void main(String[] args) {
        try (Socket socket = new Socket("localhost", 8080);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             BufferedReader consoleIn = new BufferedReader(new InputStreamReader(System.in))) {

            System.out.println("Connected to server. Enter messages (type 'exit' to quit):");

            String userInput;
            while ((userInput = consoleIn.readLine()) != null) {
                if ("exit".equalsIgnoreCase(userInput)) break;

                // 1. 发送消息到服务端
                out.println(userInput);

                // 2. 接收服务端响应
                String serverResponse = in.readLine();
                System.out.println("Server response: " + serverResponse);
            }

        } catch (UnknownHostException e) {
            System.err.println("Unknown host: " + e.getMessage());
        } catch (IOException e) {
            System.err.println("Client error: " + e.getMessage());
        }
    }
}